module.exports = () => {
    return async function error(ctx, next) {
        try {
            await next();
            if (ctx.status >= 400) {
                ctx.throw(ctx.status);
            }
        } catch (err) {
            const status = err.status || 500;
            ctx.status = status;
            switch (status) {
                case 401:
                    ctx.body = {
                        success: false,
                        message: "你没有权限"
                    }
                    break;
                case 404:
                    ctx.body = {
                        success: false,
                        message: "接口没找到"
                    }
                    break;
                case 403:
                    ctx.body = {
                        success: false,
                        message: "拒绝访问"
                    }
                    break;
                default:
                    ctx.body = {
                        success: false,
                        message: err.message
                    }
                    break;
            }
        }
    }
}